package app.action;

import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.Properties;

public class Utilities {
	private Properties appProp = null;
	private Properties queProp = null;

	public Utilities(String[] filePath) {
		String curDir = System.getProperty("user.dir")
				+ "/application.properties";
		switch (filePath.length) {
		case 1:
			this.queProp = readProperties(filePath[0]);
			this.appProp = readProperties(curDir);
			break;
		case 2:
			this.queProp = readProperties(filePath[0]);
			this.appProp = readProperties(filePath[1]);
			break;
		default:
			this.appProp = readProperties(curDir);
			break;
		}

		InputStream in = null;

		if (this.appProp == null) {
			in = getClass().getResourceAsStream(
					"/app/ui/application.properties");
			this.appProp = readProperties(in);
		}

		if (this.queProp == null) {
			in = getClass().getResourceAsStream(
					"/app/textSrc/QuestionText.properties");
			this.queProp = readProperties(in);
		}
	}

	public Properties readProperties(Object filePath) {
		Properties prop = null;
		FileInputStream in = null;

		try {
			if (filePath instanceof String) {
				File file = new File((String) filePath);
				if (file.exists()) {
					prop = new Properties();
					in = new FileInputStream(file);
					prop.load(in);
					in.close();
				}
			} else if (filePath instanceof InputStream) {
				prop = new Properties();
				prop.load((InputStream) filePath);
			} else if (filePath instanceof File) {
				prop = new Properties();
				in = new FileInputStream((File) filePath);
				prop.load(in);
				in.close();
			} else {
				return null;
			}

		} catch (FileNotFoundException e) {
			System.err.println("error read file: application.properties -- "
					+ e.getMessage());
		} catch (IOException e) {
			System.err.println("error load file properties -- "
					+ e.getMessage());
			prop = null;
		} finally {
			try {
				if (filePath instanceof InputStream) {
					((InputStream) filePath).close();
				} else if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				System.out.println(e.getMessage());
			}
		}

		return prop;
	}

	public static void addListSupport(LinkedList<String> list, String str) {
		String[] strs = str.split(";");

		for (String s : strs) {
			list.add(s);
		}
	}

	public static Color strColToCol(String strArr) {
		if (strArr.length() > 3) {
			int[] col = new int[3];
			int i = 0;
			String[] sColor = strArr.split(";");

			for (String s : sColor) {
				col[i++] = Integer.parseInt(s);
			}

			return new Color(col[0], col[1], col[2]);
		}

		System.err.println("ERROR -- Convert String to Color.");
		return Color.RED;
	}

	public Properties getAppProp() {
		return appProp;
	}

	public Properties getQueProp() {
		return queProp;
	}

	public void setAppProp(Properties appProp) {
		this.appProp = appProp;
	}

	public void setQueProp(Properties queProp) {
		this.queProp = queProp;
	}
}
